package cn.com.pansky.action.myaccount;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.exception.Url2SqlErrorException;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.action.imgContrast.ImgContrast;
import cn.com.pansky.action.logs.Log;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.LssConstant;

import com.alibaba.fastjson.JSONObject;

public class UpdPhoneNum extends CommonAction {
	private Log log = new Log();
	private CommonService cs = new CommonService();
	
	/**
	 * 我的账户-手机号修改
	 */
	public void updPhone(){
		//日志记录start
		String AF001 = log.start(" ", "UP");
		
		String sql = "select * from RC10 where AA032 = ? and aa003 = ?";
		String AA032_1 = getParam("AA032_1");
		Model mm = SqlHelper.queryOne(sql, AA032_1, user.getString("AA003"));
		System.out.println(user.getString("AA003")+"===="+user.getString("AA032"));
		JSONObject result = new JSONObject();
		if (null != mm) {
			result.put("isPass", false);
			result.put("info", "您输入的手机号已存在，请更换其他手机号");
		} else {
			//刷脸---根据身份证号查询服务对象id
			String findCA001 = urlConfig.getVar("findCA001");
			Model m = SqlHelper.queryOne(findCA001, user.getString("AA032"),user.getString("AA003"));
			String CA001 = m.getString("CA001");
			//获取刷脸图片
			String imgPath = getParam("img");
			// 指定身份证照片保存路径：
			String filename = "";
			Properties p = new Properties();
			try {
				p.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
			} catch (IOException e) {
				e.printStackTrace();
				System.out.print("---------------------");
				throw new Url2SqlErrorException(-1, "服务异常，请联系管理员");
			}
			// 指定身份证照片保存路径：
			filename = p.getProperty("imgPath") + CA001 + ".jpg";
			cs.getIdcardPic(CA001, filename);

			//调用照片比对方法，比对刷脸与身份证照片
			double num = ImgContrast.imgContrast(filename, imgPath);
			if (num>LssConstant.CONFIDENCE) {
				String updateRC10 = urlConfig.getVar("updateRC10");
				String updateRC12 = urlConfig.getVar("updateRC12");
				
				SqlHelper.execute(updateRC10, AA032_1,AA032_1, user.getString("AA032"),user.getString("AA003"));
				
				String findOldPhone = urlConfig.getVar("findOldPhone");
				List<Model> list = SqlHelper.query(findOldPhone, user.getString("AA032"));
				if (list.size()>0) {
					SqlHelper.execute(updateRC12, AA032_1, user.getString("AA032"));	
				}
				context.getServletSession().removeAttribute("user");
				result.put("isPass", true);
				result.put("info", "修改成功，请重新登陆");
			}else{
				result.put("isPass", false);
				result.put("info", "面部特征校验失败，请确保是用户本人操作！");
			}
			//将身份证临时照片删除
			File file = new File(filename);
			file.delete();
			//将刷脸图片删除
			File file2 = new File(imgPath);
			file2.delete();
		}
		//日志记录end
		boolean results = log.end(AF001, result.get("isPass"), result.get("info"));
		System.out.println("日志：修改手机号"+results);
		context.put("result", result);
	}
}